<template>
  <el-container class="box_container">
    <el-header>
      <div>
        <img src="../assets/facont.gif" alt=""/>
        <span>简易管理系统</span>
      </div>
      <el-button @click="logout" type="info">退出</el-button>
    </el-header>

    <el-container>
      <el-aside :width=" siCollapse ? '64px':'200px'">
        <div class="toggle-btn" @click="changeSize">|||</div>
        <el-menu router
                 :default-active="activePath"
                 :collapse="siCollapse"
                 :collapse-transition="false"
                 :unique-opened="true"
                 class="el-menu-vertical-demo"
                 background-color="#333744"
                 text-color="#fff"
                 active-text-color="#ffd04b">
          <el-submenu :index=" '/'+item.path" v-for=" item in menus" :key="item.id">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{ item.menuName }}</span>
            </template>
            <el-menu-item :key="child.id" :index=" '/'+child.path" v-for="child in item.items"
                          @click="saveActivePath('/'+child.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ child.menuName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>


      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menus: [],
      siCollapse: false,
      activePath: '',   //当前所在的二级菜单路由地址
      openedPaths: []   //当前打开的二级菜单
    }
  },
  created () {
    this.$http.get('async/test')
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('path')
  },
  mounted () {
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('vueMenu/vue-menu/menuList')
      this.menus = res.result
    },
    changeSize () {
      this.siCollapse = !this.siCollapse
    },
    saveActivePath (path) {
      window.sessionStorage.setItem('path', path)
      this.activePath = path
      this.openedPaths = []
      this.openedPaths.push(path)
    }
  }

}
</script>

<style lang="less" scoped>
.box_container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: white;
  font-size: 20px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.toggle-btn {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
}

</style>
